asm
  ld sp,$EFFF
  end asm

#include "./library/pacmandelay.bas"
#include "./library/pacmanputsprite.bas"
#include "./library/smsrnd.bas"

dim eee as uinteger at $E010
dim seed as uinteger at $E012
dim ee2 as uinteger at $E014
dim ee3 as uinteger at $E016
dim ee4 as uinteger at $E018
dim eiov as uinteger at $E01A

dim expos as uinteger at $E01C
dim eypos as uinteger at $E01E
dim escl as uinteger at $E020
dim esid as uinteger at $E022


sub m62putsprite(tlay as uinteger,txp as uinteger,typ as uinteger,tid as uinteger,tcl as uinteger)
  poke $C000+(tlay*8)+0,tcl           '- clut?
  poke $C000+(tlay*8)+1,int(tcl/256)  '- clut?
  poke $C000+(tlay*8)+4,tid           '- id?
  poke $C000+(tlay*8)+5,int(tid/256)  '- id?
  poke $C000+(tlay*8)+6,txp           '- x
  poke $C000+(tlay*8)+7,int(txp/256)  '- x(bit 8)
  poke $C000+(tlay*8)+2,((typ)) bxor 255          '- y
  poke $C000+(tlay*8)+3,(int((typ)/256)) bxor 255 '- y2?
  end sub

sub kungfumputchar(txp as uinteger,typ as uinteger,tvl as ubyte)
  poke $D000+txp+(typ*64),tvl
  end sub

sub kungfumputattr(txp as uinteger,typ as uinteger,tvl as ubyte)
  poke $D800+txp+(typ*64),tvl
  end sub

seed=0

  for eee=$D000 to $DFFF
    seed=smsrnd(seed)
    poke eee,seed
    next
  for eee=$D000 to $DFFF
    poke eee,eee
    next
  for eee=$D000 to $DFFF
    poke eee,2
    next
  for eee=$D000 to $DFFF
    for ee3=0 to 20:next
    poke eee,8
    next

  for eee=0 to 31
    for ee2=0 to 7
      kungfumputattr(eee+10,1+ee2,eee+(ee2*32))
      next:next

  for eee=0 to 31
    for ee2=0 to 7
      kungfumputchar(eee+10,11+ee2,eee+(ee2*32))
      next:next

  for eee=0 to 31
    for ee2=0 to 7
      kungfumputattr(eee+10,21+ee2,eee)
      kungfumputchar(eee+10,21+ee2,ee2)
      next:next



  for eee=$C000 to $C0FF
    seed=smsrnd(seed)
    'poke eee,seed
    poke eee,eee
    next

  'for ee2=160 to 191  'for ee2=0 to 511
  '  for ee3=0 to 1000:next
  '  m62putsprite(0,ee2,ee2,ee2 band 15,ee2)
  '  m62putsprite(1,ee2+32,ee2,ee2 band 15,ee2)
  '  m62putsprite(2,ee2+64,ee2,ee2 band 15,ee2)
  '  m62putsprite(3,ee2+96,ee2,ee2 band 15,ee2)
  '  m62putsprite(4,ee2+128,ee2,ee2 band 15,ee2)
  '  next

expos=160:eypos=270
escl=ee2 band 32
esid=0

do
  eiov=in(0) bxor 255
  kungfumputchar(46,20,48+(eiov mod 10))
  kungfumputchar(45,20,48+((int(eiov/10)) mod 10))
  kungfumputchar(44,20,48+((int(eiov/100)) mod 10))

  eiov=in(1) bxor 255
  kungfumputchar(46,21,48+(eiov mod 10))
  kungfumputchar(45,21,48+((int(eiov/10)) mod 10))
  kungfumputchar(44,21,48+((int(eiov/100)) mod 10))

  eiov=in(2) bxor 255
  kungfumputchar(46,22,48+(eiov mod 10))
  kungfumputchar(45,22,48+((int(eiov/10)) mod 10))
  kungfumputchar(44,22,48+((int(eiov/100)) mod 10))

  eiov=in(3) bxor 255
  kungfumputchar(46,23,48+(eiov mod 10))
  kungfumputchar(45,23,48+((int(eiov/10)) mod 10))
  kungfumputchar(44,23,48+((int(eiov/100)) mod 10))

  eiov=in(4) bxor 255
  kungfumputchar(46,24,48+(eiov mod 10))
  kungfumputchar(45,24,48+((int(eiov/10)) mod 10))
  kungfumputchar(44,24,48+((int(eiov/100)) mod 10))

  kungfumputchar(46,25,48+(expos mod 10))
  kungfumputchar(45,25,48+((int(expos/10)) mod 10))
  kungfumputchar(44,25,48+((int(expos/100)) mod 10))

  kungfumputchar(46,26,48+(eypos mod 10))
  kungfumputchar(45,26,48+((int(eypos/10)) mod 10))
  kungfumputchar(44,26,48+((int(eypos/100)) mod 10))

  kungfumputchar(46,27,48+(escl mod 10))
  kungfumputchar(45,27,48+((int(escl/10)) mod 10))
  'kungfumputchar(44,27,48+((int(escl/100)) mod 10))

  kungfumputchar(46,28,48+(esid mod 10))
  kungfumputchar(45,28,48+((int(esid/10)) mod 10))
  kungfumputchar(44,28,48+((int(esid/100)) mod 10))

  eiov=(in(1) bxor 255) bor (in(2) bxor 255)
  if (eiov band 8)<>0 then:eypos=eypos-1:end if
  if (eiov band 4)<>0 then:eypos=eypos+1:end if
  if (eiov band 2)<>0 then:expos=expos-1:end if
  if (eiov band 1)<>0 then:expos=expos+1:end if
  if (eiov band 128)<>0 then:escl=escl+1:end if
  if (eiov band 32)<>0 then:esid=esid+1:end if

  for eee=0 to 3
    for ee2=0 to 7
      m62putsprite((eee*8)+ee2,expos+(16*ee2),eypos+(16*eee),esid,escl)
      next:next

  poke $A000,expos
  poke $B000,int(expos/256)


  escl=escl band 63
  esid=esid band 255
  expos=expos band 511
  eypos=eypos band 511

  loop

